import axios from 'axios';
import Cbct from './base.js';
import md5 from 'js-md5';
axios.defaults.baseURL = 'http://adm.test.cbct.cn';
// axios.defaults.headers = {
//     'X-cbct-utk':Cbct.getCookie('token'),
//     'X-cbct-tsm':Date.parse(new Date()),
//     'X-cbct-sgn':md5(Date.parse(new Date())+md5(Cbct.getCookie('token')+Cbct.H5key)+Cbct.getCookie('salt')),
//     // 'dataType':'json',
//     // 'X-Requested-With':'XMLHttpRequest',
//     'Content-Type':'application/x-www-form-urlencoded',
//     // 'Access-Control-Allow-Origin':'*'
// };

export default {
    post: (url,data = {}) => {
        if(Cbct.getCookie('token1') == ''){
            Cbct.setCookie('token1','TOKEN');              
        }
        if(Cbct.getCookie('salt1') == ''){
            Cbct.setCookie('salt1','SALT');
        }
        axios.defaults.headers = {
            'X-cbct-domain': Cbct.getCookie('openId1'),
            'X-cbct-utk':Cbct.getCookie('token1'),
            'X-cbct-tsm':Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing')),
            'X-cbct-role':Cbct.getCookie('role1'),
            'X-cbct-sgn':md5(Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing'))+md5(Cbct.getCookie('token1')+Cbct.H5key)+Cbct.getCookie('salt1')),
            'Content-Type':'application/x-www-form-urlencoded',
            'X-cbct-os':'5',
            'X-cbct-channel-id':'1',
        };
        return new Promise((resolve, reject) => {
            axios.post(url, data)
            .then (response => {
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            })
        })
    },
    get: (url,params = {}) => {
        if(Cbct.getCookie('token1') == ''){
            Cbct.setCookie('token1','TOKEN');              
        }
        if(Cbct.getCookie('salt1') == ''){
          Cbct.setCookie('salt1','SALT');
        }
        axios.defaults.headers = {
            'X-cbct-domain': Cbct.getCookie('openId1'),
            'X-cbct-utk':Cbct.getCookie('token1'),
            'X-cbct-tsm':Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing')),
            'X-cbct-role':Cbct.getCookie('role1'),
            'X-cbct-sgn':md5(Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing'))+md5(Cbct.getCookie('token1')+Cbct.H5key)+Cbct.getCookie('salt1')),
            'Content-Type':'application/x-www-form-urlencoded',
            'X-cbct-os':'5',
            'X-cbct-channel-id':'1',
        };
        return new Promise((resolve, reject) => {
            axios.get(url, {params})
            .then (response => {
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            })
        })
    },
    put: (url,params = {}) => {
        if(Cbct.getCookie('token1') == ''){
            Cbct.setCookie('token1','TOKEN');              
        }
        if(Cbct.getCookie('salt1') == ''){
            Cbct.setCookie('salt1','SALT');
        }
        axios.defaults.headers = {
            'X-cbct-domain': Cbct.getCookie('openId1'),
            'X-cbct-utk':Cbct.getCookie('token1'),
            'X-cbct-tsm':Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing')),
            'X-cbct-role':Cbct.getCookie('role1'),
            'X-cbct-sgn':md5(Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing'))+md5(Cbct.getCookie('token1')+Cbct.H5key)+Cbct.getCookie('salt1')),
            'Content-Type':'application/x-www-form-urlencoded',
            'X-cbct-os':'5',
            'X-cbct-channel-id':'1',
        };
        return new Promise((resolve, reject) => {
            axios.put(url, {params})
            .then (response => {
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            })
        })
    }
}
